package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.vo.UserBalanceWithdrawCashConfigVO;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.UserService;
import com.yami.shop.user.common.dto.UserBalanceWithdrawCashSetDto;
import com.yami.shop.user.common.model.UserBalanceWithdrawCash;
import com.yami.shop.user.common.param.RangeTimeParam;
import com.yami.shop.user.common.service.UserBalanceWithdrawCashService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * 用户提现申请信息
 *
 * @author LGH
 * @date 2019-09-29 11:10:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/userWithdrawCash")
@Tag(name = "用户提现申请信息")
public class UserWithdrawCashController {

    private final UserBalanceWithdrawCashService userBalanceWithdrawCashService;

    private final UserService userService;

    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "分页查询")
    public ServerResponseEntity<IPage<UserBalanceWithdrawCash>> getShopWithdrawCashPage(PageParam<UserBalanceWithdrawCash> page, @ParameterObject RangeTimeParam rangeTimeParam, String userMobile, String userName, @ParameterObject UserBalanceWithdrawCash userBalanceWithdrawCash) {
        return ServerResponseEntity.success(userBalanceWithdrawCashService.userBalanceWithdrawCashsPage(page, rangeTimeParam, userMobile, userName, userBalanceWithdrawCash));
    }

    @GetMapping("/exportUserWithdrawCash")
    @Operation(summary = "导出用户提现信息", description = "导出用户提现信息")
    public void exportShopWithdrawCash(@ParameterObject UserBalanceWithdrawCash userBalanceWithdrawCash, HttpServletResponse response) {
        userBalanceWithdrawCashService.exportShopWithdrawCash(userBalanceWithdrawCash, response);
    }

    @GetMapping("/info/{cashId}")
    @Operation(summary = "查询用户提现申请信息", description = "查询用户提现申请信息")
    @Parameter(name = "cashId", description = "商家提现申请id")
    public ServerResponseEntity<UserBalanceWithdrawCash> getById(@PathVariable("cashId") Long cashId) {
        return ServerResponseEntity.success(userBalanceWithdrawCashService.getById(cashId));
    }

    @SysLog("审核用户提现信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('user:userWithdrawCash:audit')")
    @Operation(summary = "审核信息", description = "审核信息")
    public ServerResponseEntity<Void> audit(@RequestBody UserBalanceWithdrawCash userBalanceWithdrawCash) {
        UserBalanceWithdrawCash withdrawCash = userBalanceWithdrawCashService.getById(userBalanceWithdrawCash.getWithdrawCashId());
        if (withdrawCash == null) {
            // 未找到申请信息
            throw new YamiShopBindException("yami.store.apply.no.exist");
        }
        User User = userService.getUserDetail(withdrawCash.getUserId());
        if (User == null) {
            // 未找到该用户信息
            throw new YamiShopBindException("yami.store.not.exist");
        }
        userBalanceWithdrawCash.setCreateTime(null);

        userBalanceWithdrawCashService.auditWithdrawCash(userBalanceWithdrawCash.getWithdrawCashId(), userBalanceWithdrawCash, SecurityUtils.getSysUser().getUserId(), null);
        // 减少冻结金额
        return ServerResponseEntity.success();
    }

    @PostMapping("/save")
    @Operation(summary = "设置用户提现金额", description = "设置提现金额")
    public ServerResponseEntity<Void> saveWithdrawCashConfig(@RequestBody UserBalanceWithdrawCashSetDto userBalanceWithdrawCashSetDto) {
        userBalanceWithdrawCashService.saveConfig(userBalanceWithdrawCashSetDto);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getWithdrawCash")
    @Operation(summary = "获取用户提现配置", description = "获取用户提现配置")
    public ServerResponseEntity<UserBalanceWithdrawCashConfigVO> getWithdrawCashConfig() {
        return ServerResponseEntity.success(userBalanceWithdrawCashService.getConfig());
    }

}
